<?php

class MemberForm extends BaseForm {
  const FORM_NAME                 = 'frm';

  const FIELD_USER_GROUP_ID       = 'user_group_id';
  const FIELD_MERCHANT_ID         = 'merchant_id';
  const FIELD_FIRST_NAME          = 'first_name';
  const FIELD_LAST_NAME           = 'last_name';
  const FIELD_USERNAME            = 'username';
  const FIELD_EMAIL               = 'email';
  const FIELD_DATE_OF_BIRTH       = 'date_of_birth';
  const FIELD_GENDER              = 'gender';
  const FIELD_ADDRESS             = 'address';
  const FIELD_SUBURL              = 'suburb';
  const FIELD_STATE_ID            = 'state_id';
  const FIELD_POST_CODE           = 'post_code';
  const FIELD_PHONE               = 'phone';
  const FIELD_IP_ADDRESS          = 'ip_address';
  const FIELD_LATITUDE            = 'latitude';
  const FIELD_LONGITUDE           = 'longitude';
  const FIELD_TYPE                = 'type';
  const FIELD_LAST_LOGIN_AT       = 'last_login_at';
  const FIELD_STATUS              = 'status';
  const FIELD_CREATED_AT          = 'created_at';
  const FIELD_UPDATED_AT          = 'updated_at';

  const SORT_FIRST_NAME           = 'FIRST_NAME';
  const SORT_LAST_NAME            = 'LAST_NAME';
  const SORT_CREATED_AT           = 'CREATED_AT desc';

  public function configure() {
    $arrStatusList = StringUtil::getStatusArray();

    $widget = array(
            self::FIELD_STATUS        => new sfWidgetFormSelect(array('choices' => $arrStatusList), array('multiple' => false, 'expand' => true))
    );

    $labels = array(
            self::FIELD_USER_GROUP_ID   => 'User Group',
            self::FIELD_MERCHANT_ID     => 'Merchant Id',
            self::FIELD_FIRST_NAME      => 'First Name',
            self::FIELD_LAST_NAME       => 'Last Name',
            self::FIELD_USERNAME        => 'Username',
            self::FIELD_EMAIL           => 'Email',
            self::FIELD_DATE_OF_BIRTH   => 'Date of Birth',
            self::FIELD_GENDER          => 'Gender',
            self::FIELD_ADDRESS         => 'Address',
            self::FIELD_SUBURL          => 'Sub url',
            self::FIELD_STATE_ID        => 'State',
            self::FIELD_POST_CODE       => 'Post Code',
            self::FIELD_PHONE           => 'Phone',
            self::FIELD_IP_ADDRESS      => 'Ip Address',
            self::FIELD_LATITUDE        => 'Latitude',
            self::FIELD_LONGITUDE       => 'Longitude',
            self::FIELD_TYPE            => 'Type',
            self::FIELD_LAST_LOGIN_AT   => 'Last Login',
            self::FIELD_STATUS          => 'status',
            self::FIELD_CREATED_AT      => 'Create At',
            self::FIELD_UPDATED_AT      => 'Last Update'

    );

    $this->setWidgets($widget);
    $this->widgetSchema->setLabels($labels);

    $validators = array(
            self::FIELD_STATUS        => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_STATUS]))))
    );

    $this->setValidators($validators);
    $this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'postValidator'))));

    $this->widgetSchema->setNameFormat(self::FORM_NAME . '[%s]');

    $this->validatorSchema->setOption('allow_extra_fields', true);
    $this->validatorSchema->setOption('filter_extra_fields', false);
  }

  public function postValidator($validator, $values) {
    return $values;
  }

  public function populateData(USER $user) {
    $this->setDefault(self::FIELD_MERCHANT_ID, $user->getMerchantId());
    $this->setDefault(self::FIELD_STATUS, $user->getStatus());
  }
}
?>